<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="150px">
        <router-link to="/">
          <div class="title-bar">
            <img class="logo" src="../assets/logo.png" alt="logo" />
            <div class="title">Greensam</div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClick"
        />
      </a-col>
      <!--  用户信息展示栏    -->
      <a-col flex="120px">
        <div class="login">
          <div v-if="loginUserStore.loginUser.id">
            <a-dropdown>
              <a-space>
                <a-avatar :src="loginUserStore.loginUser.avatar" />
                {{ loginUserStore.loginUser.name ?? '无名' }}
              </a-space>

              <template #overlay>
                <a-menu>
                  <a-menu-item @click="toPersonalCenter">
                    <ToolOutlined style="margin-right: 10px" />
                    个人中心
                  </a-menu-item>
                  <a-menu-item @click="toUserManage">
                    <TeamOutlined style="margin-right: 10px" />
                    用户管理
                  </a-menu-item>
                  <a-menu-item @click="doLogout">
                    <LogoutOutlined style="margin-right: 10px" />
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { computed, h, ref } from 'vue'
import { HomeOutlined, LogoutOutlined, TeamOutlined, ToolOutlined } from '@ant-design/icons-vue'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { type MenuProps, message } from 'ant-design-vue'
import { userLogout } from '@/api/userController.ts'

const loginUserStore = useLoginUserStore()

// 原始菜单
const originMenu = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/about',
    label: '关于',
    title: '关于',
  },
  {
    key: '/admin/userManage',
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: 'Other',
    label: h('a', { href: 'https://blog.csdn.net/Huayudong', target: '_blank' }, '联系我'),
    title: '联系我',
  },
]

// 根据权限过滤菜单项
const filterMenu = (menus = [] as MenuProps['items']) => {
  return menus?.filter((menu) => {
    if (menu?.key?.startsWith('/admin')) {
      const loginUser = loginUserStore.loginUser
      if (
        !loginUser ||
        loginUser.roles == null ||
        !loginUser.roles.some((role) => role.value === 'admin')
      ) {
        return false
      }
    }
    return true
  })
}

// 展示在菜单的路由数组
const items = computed(() => filterMenu(originMenu))

const router = useRouter()
// 当前要高亮的菜单项
const current = ref<string[]>([])
// 监听路由变化，更新高亮菜单项
router.afterEach((to, from, failure) => {
  current.value = [to.path]
})

// 路由跳转事件
const doMenuClick = ({ key }) => {
  router.push({
    path: key,
  })
}

// 去个人中心点击事件
const toPersonalCenter = () => {
  router.push({})
}

// 去用户管理点击事件
const toUserManage = () => {
  router.push({
    path: '/admin/userManage',
  })
}

// 退出登录点击事件
const doLogout = async () => {
  const res = await userLogout()
  if (res.data.code === 0) {
    loginUserStore.setLoginUser({
      name: '未登录',
    })
    message.success('退出登录')
    await router.push('/user/login')
  } else {
    message.error('退出登录失败，' + res.data.message)
  }
}
</script>

<style scoped>
#globalHeader .title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: black;
  font-size: 18px;
  margin-left: 14px;
}

.logo {
  height: 48px;
}
</style>
